{-# LANGUAGE TemplateHaskell #-}
module MyHdf5.MyReadH5 where
-- import System.IO as IO
import Bindings.HDF5 as H5
import qualified Data.ByteString.Char8 as BS
import MyTools.MyMacros

fileName    = BS.pack "mesh.h5"
datasetName = BS.pack "Mesh/Cell/Nodes.Index"

-- 定义读取有限元网格信息的函数
readFiniteElementMesh :: FilePath -> IO ()
readFiniteElementMesh filePath = do
  $(_myLoc)
  -- 打开HDF5文件
  -- Open the file and the dataset.  
  file <- openFile fileName [] Nothing

  -- 读取网格数据集
  --dataset <- H5.getDataset file "mesh"
  dataset <- openDataset file datasetName Nothing
  

  -- 读取网格数据
  --   let meshData = readDataset dataset :: [[Double]]

  -- 输出网格数据
  --   putStrLn "Finite Element Mesh Information:"
  --   mapM_ print meshData

  -- Get datatype and dataspace handles and then query
  -- dataset class, order, size, rank and dimensions.
  datatype <- getDatasetType dataset
  t_class <- getTypeClass datatype
  putStrLn ("Data set has " ++ show t_class ++ " type")
  order <- getByteOrder datatype
  putStrLn ("Data set has byte order: " ++ show order)
    
  size <- getTypeSize datatype
  putStrLn ("Data size is " ++ show size)
    
  dataspace <- getDatasetSpace dataset
  rank <- getSimpleDataspaceExtentNDims dataspace
  (dims_out, _) <- getSimpleDataspaceExtent dataspace
  putStrLn ("rank " ++ show rank ++ ", dimensions " ++ show dims_out)

  --  Close/release resources.
  closeTypeID datatype
  closeDataset dataset
  closeDataspace dataspace
  -- closeDataspace memspace
  closeFile file

  pure ()

-- 主函数
te0 :: IO ()
te0 = do
  let filePath = "mesh.h5" -- 替换为你的HDF5文件路径
  readFiniteElementMesh filePath
